package org.com.service.impl;

import java.util.Date;

import org.com.micro.service.HelloService;
import org.com.micro.service.WorldService;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;

@Service(version = "1.0.0")
public class WorldServiceImpl implements WorldService {
	
	//@Autowired
	@Reference(version = "1.0.0", url = "dubbo://192.168.99.1:20880")
  	private HelloService helloService;

	@Override
    public String sayWorld(String name) {
		if(helloService==null) {
			initService();
		}
        return "------"+helloService.sayHello(name);
    }
	
	private void initService() {
		ApplicationConfig application = new ApplicationConfig();
		application.setName("yyy");
		 
		// 连接注册中心配置
		RegistryConfig registry = new RegistryConfig();
		registry.setAddress("192.168.80.71:2181");
		//registry.setUsername("aaa");
		//registry.setPassword("bbb");
		 
		// 注意：ReferenceConfig为重对象，内部封装了与注册中心的连接，以及与服务提供方的连接
		 
		// 引用远程服务
		ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>(); // 此实例很重，封装了与注册中心的连接以及与提供者的连接，请自行缓存，否则可能造成内存和连接泄漏
		reference.setApplication(application);
		reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
		reference.setInterface(HelloService.class);
		reference.setVersion("1.0.0");
		 
		// 和本地bean一样使用xxxService
		helloService = reference.get();
	}

}